<?php

/**
 */

/**
 * Class Query Builder for LowCostGroup API requests
 */
class lowQueryBuilder {
    private
        $login,
        $password,
        $reference;

    private $mealInfo = array(
        1 => 'All Inclusive',
        3 => 'Bed & Breakfast',
        5 => 'Full Board',
        7 => 'Half Board',
        8 => 'Room Only',
        9 => 'Self Catering',
    );

    private $cardType = array(
        1 => 'Visa Credit',
        2 => 'MasterCard',
        3 => 'Visa Debit',
        4 => 'Visa Delta',
        5 => 'Visa Electron',
        6 => 'Switch',
        7 => 'Solo',
        8 => 'Maestro',
        10 => 'American Express',
        12 => 'Dummy',
    );
    public function __construct(Array $auth=array()) {
        if (!$auth) {
            throw new InvalidArgumentException(__METHOD__.' expects an array with authentication credentials');
        }
        $this->login     = $auth['Login'];
        $this->password  = $auth['Password'];
        $this->reference = $auth['AgentReference'];
    }

    public function search(Array $params=array()) {
               
        $resorts = '';
        foreach ($params['ResortID'] as $value) {
             $resorts .= "<Resort>
                             <ResortID>".$value."</ResortID> 
                          </Resort>";            
        }
   
        $xmlStr = '<SearchRequest>
                      <LoginDetails>
                          <Login>'.$this->login.'</Login>
                          <Password>'.$this->password.'</Password>
                          <AgentReference>'.$this->reference.'</AgentReference>
                      </LoginDetails>
                      <SearchDetails>
                          <ArrivalDate>'.date('Y-m-d', $params['ArrivalDate']).'</ArrivalDate>
                          <Duration>'.$params['Duration'].'</Duration>
                          <RegionID>'.$params['RegionID'].'</RegionID>
                          <AirportCode>PMI</AirportCode>
                          <PropertyID>0</PropertyID>
                          <Resorts>'.$resorts.'</Resorts>
                          <MealBasisID>0</MealBasisID>
                          <MinStarRating>0</MinStarRating>
                          <RoomRequests>'
                             .$this->buildRoomRequestTag($params['Rooms'], $params['ChildrenAge']).
                          '</RoomRequests>
                      </SearchDetails>
                   </SearchRequest>';   
        return 'DATA='.$xmlStr;
    }
    
    private function buildRoomRequestTag($roomsRequest, $ages){
           $xmlRoomRequest = '';
           $index = 1;
           foreach ($roomsRequest as $room) {
               $xmlRoomRequest .= ' <RoomRequest>
                                        <Adults>'.$room['Adults'].'</Adults>
                                        <Children>'.$room['Children'].'</Children>
                                        <Infants>0</Infants>
                                        <ChildAges>'
                                              .$this->buildChildreenAgesTagSearch($ages, $index).
                                         '</ChildAges>
                                    </RoomRequest>';
                $index++;
               }
           return $xmlRoomRequest;
               
                                      
    }    
    
    private function buildChildreenAgesTagSearch($ages, $index){
           $xmlChildrenAgesRequest = '';
               foreach ($ages[$index] as $age){
                  $xmlChildrenAgesRequest .= '<ChildAge>
                                                  <Age>' . $age . '</Age>
                                             </ChildAge>';
                }
           return $xmlChildrenAgesRequest;
    }

    public function book(Array $params=array()) {

        $rooms = reset($params['RoomBookings']);
        $room_id = reset($params['PropertyRoomTypeID']);
        $mealBasis = array_search($rooms['rooms'][$room_id]['Board'], $this->mealInfo);
        switch (strtolower($params['CreditCardTypeID'])) {
            case 'visa':
                $ccTypeID = array_search('Visa Credit', $this->cardType);
            break;
            case 'mc':
                $ccTypeID = array_search('MasterCard', $this->cardType);
            break;
            case 'laser':
                $ccTypeID = array_search('Dummy', $this->cardType);
            break;
        }
        
      $xmlstr = '
        <BookRequest>
            <LoginDetails>
                <Login>'.$this->login.'</Login>
                <Password>'.$this->password.'</Password>
                <AgentReference>'.$this->reference.'</AgentReference>
            </LoginDetails>
            <BookingDetails>
                <PropertyID>'.$params['PropertyID'].'</PropertyID>
                <ArrivalDate>'.date('Y-m-d', $params['ArrivalDate']).'</ArrivalDate>
                <Duration>'.$params['Duration'].'</Duration>
                <LeadGuestTitle>'.$params['Guests'][1]['adults'][1]['title'].'</LeadGuestTitle>
                <LeadGuestFirstName>'.$params['Guests'][1]['adults'][1]['first_name'].'</LeadGuestFirstName>
                <LeadGuestLastName>'.$params['Guests'][1]['adults'][1]['last_name'].'</LeadGuestLastName>
                <LeadGuestAddress1>'.$params['LeadGuestAddress1'].'</LeadGuestAddress1>
                <LeadGuestTownCity>'.$params['LeadGuestTownCity'].'</LeadGuestTownCity>
                <LeadGuestPostcode>'.$params['LeadGuestPostcode'].'</LeadGuestPostcode>
                <LeadGuestEmail>'.$params['LeadGuestEmail'].'</LeadGuestEmail>
                <TradeReference>'.$params['TradeReference'].'</TradeReference>
                <ExpectedTotal>'.$params['Amount'].'</ExpectedTotal>
                <Request>'.$params['Request'].'</Request>
                <RoomBookings>'.
                   $this->buildRoomBookingTag($params)
                .'</RoomBookings>
            </BookingDetails>         
        </BookRequest>' ; 
                
        return 'DATA='.$xmlstr;
    }
    
    private function buildRoomBookingTag($params){
 
        $rooms = $params['RoomBookings'];
        $rooms_id = $params['PropertyRoomTypeID'];
        $xmlstr = '';
        foreach ($rooms as $key => $room) {
           
            $id = $rooms_id[$key];            
            
            $xmlstr = $xmlstr .'<RoomBooking>
                        <PropertyRoomTypeID>'. $room['rooms'][$id]['ID'].'</PropertyRoomTypeID>
                        <MealBasisID>'.array_search($room['rooms'][$id]['Board'], $this->mealInfo) .'</MealBasisID>
                        <Adults>'.$room['adults'].'</Adults>
                        <Children>'.$room['children'].'</Children>    
                        <Guests>'.
                            $this->buildGuestRoomBookingTag($params, $key )
                       .'</Guests>
                        </RoomBooking>';
        }        
        return $xmlstr;
    }
    
    
    
    private function buildGuestRoomBookingTag($params,$roomIndex){
             $guests = $this->buildGuestRoomGuestByTypeBookingTag($params['Guests'][$roomIndex]['adults'],  'Adult');
             $guests = $guests. $this->buildGuestRoomGuestByTypeBookingTag($params['Guests'][$roomIndex]['child'],  'Child');
             return $guests;

    }
    
    private function buildGuestRoomGuestByTypeBookingTag($params, $type){
        
        $xmlstr = '';       
        
        foreach ($params as $value) {
               if ($type == 'Child'){
                   $age = $value['age'];
               } else $age = 0;
               $xmlstr = $xmlstr. '<Guest>
                                      <Type>'.$type.'</Type>
                                      <Title>'.$value['title'].'</Title>
                                      <FirstName>'.$value['first_name'].'</FirstName>
                                      <LastName>'.$value['last_name'].'</LastName>
                                      <Age>'.$age.'</Age>
                                   </Guest>';            
        }
        return $xmlstr;             

        
    }
}
?>